const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    biz_token: '',
    lateList: {},
    flag: 1
  },

  onShow(){
    this.loadrecord()
  },

  loadrecord() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/facerecord',
      data: {
        access_token: access_token,
      },
      success: (res) => {
        console.log(res)
        if (res.data.code >= 500) {
          app.getLogin().then(function () {
            console.log('二次登录。。。')
          })
        } else if (res.data.code == 1) {
          let lateArr = res.data.list.pop()
          this.setData({
            lateList: lateArr
          })
        }
      }
    })
  },

  gotoVerify() {
    let access_token = wx.getStorageSync('access_token')
    let promise = new Promise((resolve, reject) => {
      wx.request({
        url: 'https://retire.wshoto.com/wx/getfacetoken',
        data: {
          access_token: access_token,
        },
        success: (res) => {
          console.log(res)
          if (res.data.code == 1) {
            let biz_token = res.data.biz_token
            resolve(biz_token)
          } else {
            wx.showModal({
              title: "提示",
              content: res.data.msg,
              showCancel: false
            })
          }
        }
      })
    })
    promise.then((biz_token) => {
      wx.startVerify({
        data: {
          token: biz_token // BizToken
        },
        success: (res) => { // 验证成功后触发
          // res 包含验证成功的token, 这里需要加500ms延时，防止iOS下不执行后面的逻辑
          setTimeout(() => {
            console.log(res)
            // let access_token = wx.getStorageSync('access_token')
            wx.request({
              url: 'https://retire.wshoto.com/wx/getfaceres',
              data: {
                biz_token: res.BizToken,
                access_token: access_token,
              },
              success: (res) => {
                console.log(res)
                this.loadrecord()
              }
            })
            // 验证成功后，拿到token后的逻辑处理，具体以客户自身逻辑为准
          }, 500);
        },
        fail: (err) => {  // 验证失败时触发
          // err 包含错误码，错误信息，弹窗提示错误
          setTimeout(() => {
            wx.showModal({
              title: "提示",
              content: err.ErrorMsg,
              showCancel: false
            })
          }, 500);
        }
      });
    })
  },

  toRecord() {
    wx.navigateTo({
      url: '../record/record',
    })
  }
})